package entry

import (
	"com.lh.basic.service/lib/tools"
	"com.lh.basic.service/pb/auth"
	"com.lh.basic.service/pb/menu"
	au "com.lh.basic.service/service/auth"
	m "com.lh.basic.service/service/menu"
	"com.lh.user.service/pb/user"
	"fmt"
	"google.golang.org/grpc"
	"log"
	"net"
	"sync"
)

func InitGrpc(opts *user.ServeConfig, wg *sync.WaitGroup) {
	defer wg.Done()
	serve, err := net.Listen("tcp", fmt.Sprintf("%s:%d", opts.Host, opts.Port))
	if err != nil {
		log.Printf("failed to listen: %v", err)
	}
	grpcServer := grpc.NewServer(grpc.UnaryInterceptor(tools.AuthFilterInterceptor))
	menu.RegisterMenuServiceServer(grpcServer, &m.Menu{})
	auth.RegisterAuthServiceServer(grpcServer, &au.Auth{})
	log.Printf("gRPC server listening on: %s", serve.Addr())
	if err = grpcServer.Serve(serve); err != nil {
		log.Printf("failed to serve: %v", err)
	}
}
